package top.jolyoulu.codegenweb.service.bll.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jolyoulu.codegenweb.entity.bo.form.MysqlTableColumnMapFormBO;
import top.jolyoulu.codegenweb.entity.bo.query.MysqlTableColumnMapQueryBO;
import top.jolyoulu.codegenweb.entity.po.MysqlTableColumnMap;
import top.jolyoulu.codegenweb.entity.vo.MysqlTableColumnMapVO;
import top.jolyoulu.codegenweb.service.bll.ApiMysqlTableColumnMapService;
import top.jolyoulu.codegenweb.service.dal.MysqlTableColumnMapService;
import top.jolyoulu.modules.mybatisplusmodule.plugin.page.JlPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: JolyouLu
 * @Date: 2023/4/15 14:16
 * @Description
 */
@Slf4j
@Service
public class ApiMysqlTableColumnMapServiceImpl implements ApiMysqlTableColumnMapService {

    @Autowired
    private MysqlTableColumnMapService mysqlTableColumnMapService;

    @Override
    public JlPage<MysqlTableColumnMapVO> pageList(MysqlTableColumnMapQueryBO bo) {
        JlPage<MysqlTableColumnMapVO> res = JlPage.execute(bo.getPage(), bo.getPageSize(),
                () -> {
                    LambdaQueryWrapper<MysqlTableColumnMap> ew = new LambdaQueryWrapper<>();
                    List<MysqlTableColumnMap> list = mysqlTableColumnMapService.list(ew);
                    return list.stream().map(MysqlTableColumnMapVO::new).collect(Collectors.toList());
                });
        return res;
    }

    @Override
    public MysqlTableColumnMapVO getById(String id) {
        MysqlTableColumnMap res = mysqlTableColumnMapService.getById(id);
        return new MysqlTableColumnMapVO(res);
    }

    @Override
    @Transactional
    public void add(MysqlTableColumnMapFormBO bo) {
        MysqlTableColumnMap mysqlTableColumnMap = new MysqlTableColumnMap();
        BeanUtils.copyProperties(bo,mysqlTableColumnMap);
        mysqlTableColumnMapService.save(mysqlTableColumnMap);
    }

    @Override
    @Transactional
    public void update(MysqlTableColumnMapFormBO bo) {
        MysqlTableColumnMap mysqlTableColumnMap = new MysqlTableColumnMap();
        BeanUtils.copyProperties(bo,mysqlTableColumnMap);
        mysqlTableColumnMapService.updateById(mysqlTableColumnMap);
    }

    @Override
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            mysqlTableColumnMapService.removeById(id);
        }
    }
}
